package com.sunda.spmswms.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sunda.spmswms.entity.InventoryCheckAction;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 仓库盘盈盘亏 Mapper 接口
 * </p>
 *
 * @author Wayne
 * @since 2021-05-10
 */
@Mapper
@Component
public interface InventoryCheckActionMapper extends BaseMapper<InventoryCheckAction> {

    @Update("UPDATE INVENTORY_CHECK_ACTION SET SPMS_STATUS = #{spmsStatus}, \n" +
            "SAP_VOUCHER_NUMBER = #{SAP_VOUCHER_NUMBER}, \n" +
            "SAP_VOUCHER_YEAR = #{SAP_VOUCHER_YEAR}, \n" +
            "POSTING_DATE = #{postingDate}, \n" +
            "REMARKS = #{remarks} " +
            "WHERE UUID = #{uuid} ")
    int updateInventoryCheckActionStatus(@Param("uuid") String uuid,
                                         @Param("spmsStatus") String spmsStatus,
                                         @Param("SAP_VOUCHER_NUMBER") String SAP_VOUCHER_NUMBER,
                                         @Param("SAP_VOUCHER_YEAR") String SAP_VOUCHER_YEAR,
                                         @Param("postingDate") String postingDate,
                                         @Param("remarks") String remarks);

    @Select("SELECT A.WERKS AS \"werks\",\n" +
            "       A.WHS_LOCATION_CODE AS \"whsLocationCode\",\n" +
            "       A.INVENTORY_ACTION_TYPE AS \"inventoryActionType\",\n" +
            "       AD.UUID AS \"uuid\",\n" +
            "       AD.TASK_ROW_ID AS \"taskRowId\",\n" +
            "       AD.MATERIAL_NO AS \"materialNo\",\n" +
            "       AD.BOX_NOTE AS \"boxNote\",\n" +
            "       AD.INVENTORY_QUANTITY AS \"inventoryQuantity\",\n" +
            "       AD.CHECK_QUANTITY AS \"checkQuantity\",\n" +
            "       ABS(AD.DISCREPANCY) AS \"discrepancy\",\n" +
            "       AD.STORAGE_UUID AS \"storageUuid\"\n" +
            "FROM INVENTORY_CHECK_ACTION_DTL AD\n" +
            "         LEFT JOIN INVENTORY_CHECK_ACTION A ON AD.UUID = A.UUID\n" +
            "WHERE A.UUID = #{uuid} AND AD.DISCREPANCY != 0 ")
    List<Map<String, Object>> getInventoryCheckActionAndDtl(@Param("uuid") String uuid);

}
